/**
 * 
 */
package com.owen.he.jee.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import com.ibatis.common.util.PaginatedList;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.owen.he.jee.dao.impl.ibatis.BaseiBatisDao;
import com.owen.he.jee.dao.impl.ibatis.support.SqlMapClientCallback;

/**
 * com.tiancom.pas.framework.ibatis <br>
 * 说明：DAO基类<br>
 * <br>
 * 
 * @author: meteor 创建时间 2007-3-20 下午02:35:50
 */
public class PasBaseDAO extends BaseiBatisDao {

	/**
	 * 添加
	 * 
	 * @param record
	 */
	public void insert(String sqlKey, Object pojo) {
		getSqlMapClientTemplate().insert(sqlKey, pojo);
	}

	/**
	 * 修改
	 * 
	 * @param record
	 * @return
	 */
	public int updateByPrimaryKey(String sqlKey, Object key) {
		int rows = getSqlMapClientTemplate().update(sqlKey, key);
		
		
		return rows;
	}

	/**
	 * 根据主键查询
	 * 
	 * @param khdxdh
	 * @return
	 */
	public Object selectByPrimaryKey(String sqlKey, Object key) {
		return getSqlMapClientTemplate().queryForObject(sqlKey, key);
	}

	/**
	 * 根据主键删除一条记录
	 * 
	 * @param khdxdh
	 * @return
	 */
	public int deleteByPrimaryKey(String sqlKey, Object key) {
		int rows = getSqlMapClientTemplate().delete(sqlKey, key);
		return rows;
	}

	/**
	 * 根据条件查询对象集合
	 * 
	 * @param map
	 *            查询条件集合
	 * @param sqlKey
	 *            对应sqlMap中的的属性id
	 * @return 对象集合
	 */
	public List selectInfoByPara(String sqlKey, Map map) {
		List list = getSqlMapClientTemplate().queryForList(sqlKey, map);
		return list;
	}

	/**
	 * 根据条件查询对象集合
	 * 
	 * @param sqlKey
	 *            sqlKey 对应sqlMap中的的属性id
	 * @param para
	 *            查询条件
	 * @return
	 */
	public List selectInfoByPara(String sqlKey, String para) {
		List list = getSqlMapClientTemplate().queryForList(sqlKey, para);
		return list;
	}

	/**
	 * 根据条件查询分页数据
	 * 
	 * @param map
	 * @param sqlKey
	 * @param begin
	 * @param pageSize
	 * @param id
	 * @return
	 */
	public List selectPageInfoByPara(String sqlKey, Map map, int starRow,
			int pageSize) {
		List list = null;
		list =  getSqlMapClientTemplate().queryForList(sqlKey, map,starRow, pageSize);
		return list;
		
	}

	public List selectPageInfoByParaForSp(String sqlKey, Map map, int starRow,
			int pageSize) {
		
		List list = getSqlMapClientTemplate().queryForList(sqlKey, map,
				starRow, pageSize);
		return list;
	}

	/**
	 * 根据条件查询记录总条数
	 * 
	 * @param map
	 * @param sqlKey
	 * @return
	 */
	public Integer selectCountRows(String sqlKey, Map map) {
		Integer count = (Integer) getSqlMapClientTemplate().queryForObject(
				sqlKey, map);
		return count;
	}

	/**
	 * 根据页大小查询分页数据
	 * 
	 * @param map
	 * @param sqlKey
	 * @param pageSize
	 * @return
	 */
	public PaginatedList selectPage(String sqlKey, Map map, int pageSize) {
		PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(
				sqlKey, map, pageSize);
		return list;
	}

	public Map selectParaByName(String name) {
		String sqlKey = "";
		return getSqlMapClientTemplate().queryForMap(sqlKey, name, sqlKey);

	}

	/**
	 * 根据参数查询对象列表(一个参数)
	 * 
	 * @param para
	 *            查询对象（对应sqlMap中的parameterClass）
	 * @param sqlKey
	 *            对应sqlMap中的的属性id
	 * @return
	 */
	public List selectListByPara(String sqlKey, Object para) {
		return getSqlMapClientTemplate().queryForList(sqlKey, para);
	}

	public List selectPageInfoByParaNolazy(String sqlKey, Map map, int starRow,
			int pageSize) {

		List list = getSqlMapClientTemplate().queryForList(sqlKey, map,
				starRow, pageSize);
		return list;
		// getSqlMapClientTemplate().setLazyInit(false);
		// List list = getSqlMapClientTemplate().queryForList(sqlKey, map,
		// starRow, pageSize);
		// int endRow = starRow + pageSize;
		// if (endRow > list.size()) {
		// endRow = list.size();
		// }
		// List pageList = list.subList(starRow, endRow);
		// return pageList;
	}
	
	
	
	public void batchInsert(final String sqlKey, final List<Object> list) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (Object obj : list) {
					executor.insert(sqlKey, obj);
				}
				return executor.executeBatch();
			}

		});
	}
	
	public void batchUpdate(final String sqlKey, final List<Object> list) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (Object obj : list) {
					executor.update(sqlKey, obj);
				}
				return executor.executeBatch();
			}

		});
	}

	
	/**
	 * 根据keyPro，返回Map
	 * @param sqlKey SQLMapID
	 * @param para 参数 
	 * @param keyPro 作为主键的属性
	 * @return
	 */
	public Map selectMapByPara(String sqlKey, Object para,String keyPro) {
		return getSqlMapClientTemplate().queryForMap(sqlKey, para, keyPro);
	}

	/**
	 * 根据keyPro，返回Map
	 * @param sqlKey SQLMapID
	 * @param para 参数 
	 * @param keyPro 作为主键的属性
	 * @param valuePro 作为值的属性
	 * @return
	 */
	public Map selectMapByPara(String sqlKey, Object para,String keyPro,String valueProp) {
		return getSqlMapClientTemplate().queryForMap(sqlKey, para,keyPro,valueProp);
	}
	public DataSource getSystemDataSource() {
		return this.getDataSource();
	}
	
	public Object queryForObject(String sqlKey, Object para){
		return getSqlMapClientTemplate().queryForObject(sqlKey, para);
	}
	
	public Object queryForObject(String sqlKey, Object para,String db){
		return getSqlMapClientTemplate(db).queryForObject(sqlKey, para);
	}

}